package controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import interceptor.AdminLoginInterceptor;
import model.Admin;
import model.User;
import service.*;
import service.impl.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminController extends BaseController {
    @Before(AdminLoginInterceptor.class)
    public void index() {
        render("/admin/adminIndex.jsp");
    }



    public void test(){
        System.out.println(getModel(User.class));
    }


    public void admins(){
        IAdminService adminService = new AdminServiceImpl();
        renderData(adminService);
    }


    public void getColumns(){
//        String tableId = getPara("table_id");
//        IAdminService adminService = new AdminServiceImpl();
//        if(tableId!=null && adminService.findAllTables().contains(tableId)) {
//            renderJson(adminService.findColumnsByTable(tableId));
//        }else {
//            renderError(400);
//        }
        List<String> fields = new ArrayList<String>(){{
            add("id");
            add("name");
            add("pwd");
        }};
        List<String> titles = new ArrayList<String>(){{
            add("标识");
            add("名字");
            add("密码");
        }};
        renderColumns(fields, titles);
    }

    public void addDialog() {
        render("/admin/adminAddDialog.jsp");
    }

    public void updateDialog() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IAdminService adminService = new AdminServiceImpl();
        Admin admin = (Admin) adminService.findById(id);
        setAttr("admin",admin);
        render("/admin/adminUpdateDialog.jsp");
    }

    public void save() {
        Admin admin = getModel(Admin.class);
        IAdminService adminService = new AdminServiceImpl();
        Admin result = (Admin) adminService.save(admin);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void update() {
        Admin admin = getModel(Admin.class);
        IAdminService adminService = new AdminServiceImpl();
        Admin result = (Admin) adminService.update(admin);
        int id = result != null ? result.getId() : -1;
        renderSaveResult(id, result);
    }

    public void delete() {
        Integer id = getParaToInt("id");
        //没有id参数则返回401
        if(id==null) renderError(401);
        IAdminService adminService = new AdminServiceImpl();
        boolean result = adminService.delete(id);
        renderDelResult(result);
    }

    public void login(){
        boolean loginError = getParaToBoolean("loginError",false);
        setAttr("loginError",loginError);
        render("/admin/login.jsp");
    }

    public void loginService(){
        String username = getPara("username");
        String password = getPara("password");
        System.out.println(username+" "+password);
        IAdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.login(username,password);
        if(admin!=null){
            getSession().setAttribute("adminLogin",true);
            getSession().setAttribute("admin",admin);
            redirect("/admin");
        }else{
            redirect("/admin/login?loginError=true");
        }
    }

    public void logout(){
        getSession().removeAttribute("adminLogin");
        getSession().removeAttribute("admin");
        redirect("/admin/login");
    }

}